package com.ying.user.entity.dto;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ying.user.entity.MmPermission;
import com.ying.user.entity.MmRole;
import com.ying.user.entity.MmUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class MmUserDetails implements UserDetails {

    /**
     * 自增主键
     */
    private Long id;

    /**
     * 手机号
     */
    private String telephone;

    /**
     * 密码
     */
    @JsonIgnore
    private String password;

    /**
     * 头像地址
     */
    private String avatar;

    /**
     * 性别（0：女、1：男）
     */
    private Byte gender;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 修改时间
     */
    private String updateTime;

    /**
     * 用户角色列表
     */
    private List<String> roles;

    /**
     * 用户权限列表
     */
    private Collection<? extends GrantedAuthority> authorities;

    public MmUserDetails() {
    }

    public MmUserDetails(Long id, String telephone, String password, String avatar, Byte gender, String email, String createTime, String updateTime, List<String> roles, Collection<? extends GrantedAuthority> authorities) {
        this.id = id;
        this.telephone = telephone;
        this.password = password;
        this.avatar = avatar;
        this.gender = gender;
        this.email = email;
        this.createTime = createTime;
        this.updateTime = updateTime;
        this.roles = roles;
        this.authorities = authorities;
    }

    public static MmUserDetails create(MmUser mmUser, List<MmRole> roleList, List<MmPermission> permissionList) {
        List<String> roleNames = roleList.stream()
                .map(MmRole::getName)
                .collect(Collectors.toList());
        List<GrantedAuthority> authorities = permissionList.stream()
                .filter(permission -> StrUtil.isNotBlank(permission.getPermission()))
                .map(permission -> new SimpleGrantedAuthority(permission.getPermission()))
                .collect(Collectors.toList());
        return new MmUserDetails(
                mmUser.getId(), mmUser.getTelephone(),
                mmUser.getPassword(), mmUser.getAvatar(),
                mmUser.getGender(), mmUser.getEmail(),
                mmUser.getCreateTime(), mmUser.getUpdateTime(),
                roleNames, authorities);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return telephone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Byte getGender() {
        return gender;
    }

    public void setGender(Byte gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
}
